package com.imooc.articles_mongodb_project.service.impl;

import com.imooc.articles_mongodb_project.common.Result;
import com.imooc.articles_mongodb_project.dao.ArticlesRepository;
import com.imooc.articles_mongodb_project.dao.CommentsRepository;
import com.imooc.articles_mongodb_project.dto.CommentDTO;
import com.imooc.articles_mongodb_project.entity.Articles;
import com.imooc.articles_mongodb_project.entity.Comments;
import com.imooc.articles_mongodb_project.service.ICommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;

import java.util.*;


@Service
public class CommentServiceImpl implements ICommentsService {
    @Autowired
    private CommentsRepository commentsRepository;

    @Autowired
    private ArticlesRepository articlesRepository;

    @Override
    public Result getAll() {
        return new Result(200, "查询成功", commentsRepository.findAll());
    }

    @Override
    public Result save(@org.jetbrains.annotations.NotNull CommentDTO commentDTO) {

        Optional<Articles> articlesOptional = articlesRepository.findById(commentDTO.getArticleId());
        if (!articlesOptional.isPresent()) {
            return new Result(400, "文章不存在");
        }
        Comments comments = new Comments();
        comments.setArticleId(commentDTO.getArticleId())
                .setContent(commentDTO.getContent())
                .setDate(new Date())
                .setUserName(commentDTO.getUserName())
                .setParentId(commentDTO.getParentId() != null ? commentDTO.getParentId() : "0");
        commentsRepository.insert(comments);
        if (comments.getId() != null) {
            return new Result(200, "评论成功", comments.getId());
        }
        return new Result(400, "评论失败");
    }


    public Result getAllArticlesComments(String articleId) {
        Comments comments = new Comments();
        comments.setArticleId(articleId).setParentId("0");
        Example<Comments> commentsExample = Example.of(comments);
        List<Comments> commentsList = commentsRepository.findAll(commentsExample);
        return new Result(200, "查询成功", commentsList);
    }

    public Result getReplyComments(String id) {
        Comments comments = new Comments();
        comments.setParentId(id);
        Example<Comments> commentsExample = Example.of(comments);
        List<Comments> commentsList = commentsRepository.findAll(commentsExample);
        return new Result(200, "查询成功", commentsList);
    }

    private List getCOmments(String id, List<Comments> list) {
        Comments comments = new Comments();
        comments.setParentId(id);
        Example<Comments> commentsExample = Example.of(comments);
        List<Comments> commentsList = commentsRepository.findAll(commentsExample);
        list.addAll(commentsList);
        if (!commentsList.isEmpty()) {
            for (Comments comment : commentsList) {
                getCOmments(comment.getId(), list);
            }
        }
        return list;
    }


    public Result removeComments(String id) {
        Optional<Comments> commentsOptional = commentsRepository.findById(id);
        if (!commentsOptional.isPresent()) {
            return new Result(400, "评论不存在");
        }

        ArrayList<Comments> comments = new ArrayList<>();
        comments.add(commentsOptional.get());
        getCOmments(id, comments);
        commentsRepository.deleteAll(comments);
        Optional<Comments> commentsRepositoryById = commentsRepository.findById(id);
        if (commentsRepositoryById.isPresent()) {
            return new Result(400, "删除失败");
        }
        return new Result(200, "删除成功");

    }
}
